package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.dto.ScheDto;
import com.hdax.dm.dto.SeatInfoDto;
import com.hdax.dm.dto.SeatPriceDto;
import com.hdax.dm.mappers.SchedulerMapper;
import com.hdax.dm.mapstructs.SeatMapper;
import com.hdax.dm.scheduler.DmScheduler;
import com.hdax.dm.scheduler.DmSchedulerSeat;
import com.hdax.dm.scheduler.DmSchedulerSeatPrice;
import com.hdax.dm.service.SchedulerPriceService;
import com.hdax.dm.service.SchedulerSeatSevrice;
import com.hdax.dm.service.SchedulerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class SchedulerServiceImpl extends ServiceImpl<SchedulerMapper, DmScheduler> implements SchedulerService {
    @Override
    public List<DmScheduler> schedulers(String itemId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("itemId",itemId);
        return list(wrapper);
    }

    @Resource
    private SchedulerSeatSevrice seatSevrice;

    @Resource
    private SchedulerPriceService priceService;

    @Resource
    private SeatMapper seatMapper;
    @Override
    public ScheDto scheDtos(String scheduleId) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("id",scheduleId);
        List<DmScheduler> schedulers = list(wrapper);
        ScheDto scheDto = new ScheDto();

        schedulers.forEach(sche->{
            scheDto.setScheduleId(sche.getId());
            scheDto.setCinemaId(sche.getCinemaId());
            //座位显示状态
            List<DmSchedulerSeat> schedulerSeats = seatSevrice.seatList(sche.getId());
            List<SeatInfoDto> seatInfoDtos = seatMapper.seatInfo(schedulerSeats);


            AtomicInteger i= new AtomicInteger();
            seatInfoDtos.forEach(s->{
                s.setCinemaId(sche.getCinemaId());
                String name ="";
                    if (schedulerSeats.get(i.get()).getAreaLevel() == 1L){
                        name = "A";
                    }else if (schedulerSeats.get(i.get()).getAreaLevel() == 2L){
                        name = "B";
                    }else if (schedulerSeats.get(i.get()).getAreaLevel() == 3L){
                        name = "C";
                    }
                i.getAndIncrement();
                    s.setAreaLevel(name);
            });
            scheDto.setSeatInfoList(seatInfoDtos);

            //座位价格
            List<DmSchedulerSeatPrice> schedulerSeatPrices = priceService.SchedulerPrices(sche.getId()+"");
            List<SeatPriceDto> priceDtos = seatMapper.seatPrice(schedulerSeatPrices);
            priceDtos.forEach(p->{
                String name ="";
                if (p.getAreaLevel() == 1L){
                    name = "A";
                }else if (p.getAreaLevel() == 2L){
                    name = "B";
                }else if (p.getAreaLevel() == 3L){
                    name = "C";
                }
                p.setAreaLevelName(name);
            });
            scheDto.setSeatPriceList(priceDtos);
        });

        return scheDto;
    }
}
